package cn.itcast.wushangxiantu.BaseAdapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.google.gson.Gson;

import java.io.IOException;

import cn.itcast.wushangxiantu.Activity.Login;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import cn.itcast.wushangxiantu.UserData.GuoJiaUser;
import cn.itcast.wushangxiantu.UserData.User;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class GuoJiaChengYuanAdapter extends BaseAdapter {
    private Context context;
    private GuoJiaUser guoJiaUser;
    private ViewHolder viewHolder;

    public GuoJiaChengYuanAdapter(Context context) {
        this.context = context;
    }

    public GuoJiaUser getGuoJiaUser() {
        return guoJiaUser;
    }

    public void setGuoJiaUser(GuoJiaUser guoJiaUser) {
        this.guoJiaUser = guoJiaUser;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (null == guoJiaUser) {
            return 0;
        }
        return guoJiaUser.getGuoJiaUser().size();
    }

    @Override
    public Object getItem(int i) {
        return getGuoJiaUser().getGuoJiaUser().get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = View.inflate(context, R.layout.adapter_guojiachengyuan, null);
        viewHolder=new ViewHolder(view);
        viewHolder.tvDJ.setText("LV:"+guoJiaUser.getGuoJiaUser().get(i).getDj());
        viewHolder.tvName.setText(guoJiaUser.getGuoJiaUser().get(i).getName());
        viewHolder.tvGongXian.setText("贡献"+guoJiaUser.getGuoJiaUser().get(i).getGx());
        switch (guoJiaUser.getGuoJiaUser().get(i).getJb()){
            case 0:
                viewHolder.tvZhiWei.setText("成员");
                viewHolder.tvZhiWei.setTextColor(ContextCompat.getColor(context, R.color.wuqi0));
                break;
            case 1:
                viewHolder.tvZhiWei.setText("使者");
                viewHolder.tvZhiWei.setTextColor(ContextCompat.getColor(context, R.color.wuqi1));
                break;
            case 2:
                viewHolder.tvZhiWei.setText("长老");
                viewHolder.tvZhiWei.setTextColor(ContextCompat.getColor(context, R.color.wuqi2));
                break;
            case 3:
                viewHolder.tvZhiWei.setText("副盟主");
                viewHolder.tvZhiWei.setTextColor(ContextCompat.getColor(context, R.color.wuqi3));
                break;
            case 4:
                viewHolder.tvZhiWei.setText("盟主");
                viewHolder.tvZhiWei.setTextColor(ContextCompat.getColor(context, R.color.wuqi4));
                break;
        }
        return view;
    }

    class ViewHolder implements View.OnClickListener {
        public View rootView;
        public TextView tvName;
        public TextView tvDJ;
        public TextView tvGongXian;
        public TextView tvZhiWei;
        public Button btnDelete;
        public Button btnSet;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tvName = (TextView) rootView.findViewById(R.id.tvName);
            this.tvDJ = (TextView) rootView.findViewById(R.id.tvDJ);
            this.tvGongXian = (TextView) rootView.findViewById(R.id.tvGongXian);
            this.tvZhiWei = (TextView) rootView.findViewById(R.id.tvZhiWei);
            this.btnDelete = (Button) rootView.findViewById(R.id.btnDelete);
            this.btnSet = (Button) rootView.findViewById(R.id.btnSet);
            btnDelete.setOnClickListener(this);
            btnSet.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btnDelete:
                    GeneralDialog("仙盟管理","确定要T掉此人?这样会扣除仙盟一些资金",1);
                    break;
                case R.id.btnSet:
                    Dialogs();
                    break;
            }
        }
        public void GeneralDialog(String head, String content, final int num) {
            /**
             * 普通对话框
             * 1标题
             * 2内容
             */
            AlertDialog dialog;
            AlertDialog.Builder builder = new AlertDialog.Builder(context)
                    .setTitle(head)

                    .setMessage(content)
                    .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                           switch (num){
                               case 1:
                                   deleteGuoJiaUser(Login.getUser().getUsernumber(),Login.getUser().getPassword(),tvName.getText().toString().trim());
                                   break;
                           }
                            dialogInterface.dismiss();
                        }
                    });
            dialog = builder.create();
            dialog.show();
        }
        public void Dialogs() {
            final int[] num = {0};
            AlertDialog dialog;
            AlertDialog.Builder builder = new AlertDialog.Builder(context)
                    .setTitle("成员操作")
                    .setSingleChoiceItems(new String[]{"让位盟主", "赋予副盟主", "赋予长老", "赋予使者","降至成员"}, 0, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            num[0] = i;
                        }
                    })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            updateGuoJiaUser(Login.getUser().getUsernumber(),Login.getUser().getPassword(),tvName.getText().toString().trim(),4-num[0]);
                        }
                    });
            dialog = builder.create();
            dialog.show();
        }
        private void updateGuoJiaUser(String usernumber, String password, String trim,int num) {
            OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "&need=updateGuoJiaUser" + "&userNumber=" + usernumber + "&passWord=" + password + "&name=" + trim+"&num="+num, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    try {
                        Log.v("deleteGuoJiaUser",e.getMessage());
                    }catch (Exception e1){

                    }
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String res=response.body().string();
                    Log.v("getGuoJiaUser",res);
                    Message message=new Message();
                    User user=new Gson().fromJson(res,User.class);
                    message.obj=user;
                    message.what=1;
                    handler.sendMessage(message);
                }
            });
        }
        private void deleteGuoJiaUser(String usernumber, String password, String trim) {
            OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "&need=deleteGuoJiaUser" + "&userNumber=" + usernumber + "&passWord=" + password + "&name=" + trim, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    try {
                        Log.v("deleteGuoJiaUser",e.getMessage());
                    }catch (Exception e1){

                    }
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String res=response.body().string();
                    Log.v("getGuoJiaUser",res);
                    Message message=new Message();
                    User user=new Gson().fromJson(res,User.class);
                    message.obj=user;
                    message.what=1;
                    handler.sendMessage(message);
                }
            });
        }
        Handler handler =new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {
                switch (message.what){
                    case 1:
                        User user= (User) message.obj;
                        if (null!=user){
                            GeneralDialog("操作详情",user.getStw(),-1);
                        }else {
                            ToastUtil.toast(context);
                        }
                        break;
                }
                return false;
            }
        });
    }
}
